﻿using FlowDesigner.Flow;
using FlowDesigner.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace FlowDesigner.HelperVision
{
    public class XMLserialization
    {
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public static bool Savexaml<T>(T uIElement,string path)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);//创建这个文件夹
                }
                // 确保路径拼接正确
                string filePath = System.IO.Path.Combine(path, "Ffow.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    serializer.Serialize(writer, uIElement);
                    return true;
                }
            }catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <returns></returns>
        public static List<T> Deserializationxaml<T>(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
            using (StreamReader reader = new StreamReader(fileName))
            {
                return (List<T>)serializer.Deserialize(reader);
            }

        }
        /// <summary>
        /// 流程节点控件转为实体信息
        /// </summary>
        public static List<FloWTask> NodsUItoXml(UIElementCollection Children,Canvas canvas)
        {
            List <FloWTask > floWs = new List <FloWTask>();
            foreach (var item in Children)
            {
                    if(item is NodeS)
                    {
                    var NodeS_= (NodeS)item;

                    // 获取相对于 Canvas 的坐标
                    var position = NodeS_.TransformToAncestor(canvas)
                                         .Transform(new Point(0, 0));
                    NodeS_.ParentItemsControl.NodeSX=position.X; 
                    NodeS_.ParentItemsControl.NodeSY=position.Y;
                    floWs.Add(NodeS_.ParentItemsControl);
                    }
            }
            return floWs;
        }

    }
}
